package com.neudu.yiyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yiyang.commons.JSONReturn;
import com.neudu.yiyang.pojo.Customer;
import com.neudu.yiyang.pojo.Outward;
import com.neudu.yiyang.service.CustomerService;
import com.neudu.yiyang.service.OutwardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class OutwardController {
    @Autowired
    private OutwardService outwardService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private JSONReturn jsonReturn;

    // 查询所有外出记录
    @PostMapping("get_all_outward")
    public String getAll(@RequestBody Map<String, Object> pageInfo) {
        try {
            Integer pageNum = (Integer) pageInfo.get("pageNum");
            Integer pageSize = (Integer) pageInfo.get("pageSize");

            PageHelper.startPage(pageNum, pageSize);
            List<Outward> list = outwardService.list();

            // 获取客户信息并关联到外出记录
            list.forEach(outward -> {
                Customer customer = customerService.getById(outward.getCustomerId());
                if (customer != null) {
                    outward.setCustomerName(customer.getCustomerName());
                }
            });

            PageInfo<Outward> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 根据条件查询外出记录
    @PostMapping("get_outward_by")
    public String getByCondition(@RequestBody Map<String, Object> params) {
        try {
            String customerName = (String) params.get("customerName");
            Integer pageNum = (Integer) params.get("pageNum");
            Integer pageSize = (Integer) params.get("pageSize");

            PageHelper.startPage(pageNum, pageSize);
            LambdaQueryWrapper<Outward> queryWrapper = new LambdaQueryWrapper<>();

            if (customerName != null && !customerName.isEmpty()) {
                queryWrapper.inSql(Outward::getCustomerId, "SELECT id FROM customer WHERE customer_name LIKE '%" + customerName + "%'");
            }

            List<Outward> outwards = outwardService.list(queryWrapper);

            // 获取客户信息并关联到外出记录
            outwards.forEach(outward -> {
                Customer customer = customerService.getById(outward.getCustomerId());
                if (customer != null) {
                    outward.setCustomerName(customer.getCustomerName());
                }
            });

            PageInfo<Outward> pageInfo1 = new PageInfo<>(outwards);
            return jsonReturn.returnSuccess(pageInfo1);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 新增一条外出记录
    @PostMapping("add_one_outward")
    public String addOne(@RequestBody Outward outward) {
        try {
            outward.setAuditstatus(0); // 初始状态为已提交
            outward.setIsDeleted(0);   // 初始状态为未删除
            outwardService.save(outward);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 根据ID查询外出记录
    @GetMapping("get_outward_by_id")
    public String getById(@RequestParam("id") Integer id) {
        try {
            Outward outward = outwardService.getById(id);
            // 获取客户信息并关联到外出记录
            if (outward != null) {
                Customer customer = customerService.getById(outward.getCustomerId());
                if (customer != null) {
                    outward.setCustomerName(customer.getCustomerName());
                }
            }
            return jsonReturn.returnSuccess(outward);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 修改一条外出记录
    @PutMapping("modify_outward_by_id")
    public String modifyById(@RequestBody Outward outward) {
        try {
            outwardService.updateById(outward);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 删除一条外出记录
    @DeleteMapping("remove_outward_by_id")
    public String removeById(@RequestParam("id") Integer id) {
        try {
            outwardService.removeById(id);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 审核外出记录
    @PutMapping("audit_outward")
    public String auditOutward(@RequestBody Outward outward) {
        try {
            outward.setAudittime(new java.util.Date());
            outwardService.updateById(outward);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}